import { Toast } from 'vant'
import {interfaceUtils} from './interface.js'
let crypto = require('crypto');
// https://www.npmjs.com/package/jsencrypt?activeTab=versions
import JsEncrypt from "jsencrypt";
class WaybillDetail extends interfaceUtils{
    static IsRequire=2
    static Name="waybillDetail"
    static TimeStamp=new Date().getTime()
    constructor(phone, waybillNumber ){
        super()
        this.phone=phone==""?"null":phone 
        this.waybillNumber=waybillNumber
        this.timeStamp=new Date().getTime()
       
    }

    _getParams(){
        const publicKey=WaybillDetail.publicKey
        let rsa = new JsEncrypt();
        rsa.setPublicKey(publicKey);
        const { phone } =this
        const consumerFrom=WaybillDetail.consumerFrom      
        const params=JSON.stringify( { phone}) 
        console.log("params",params)
        const result= rsa.encrypt(params)  
         //    https://www.cnblogs.com/aeolian/p/12133598.html
        return encodeURIComponent(result)      
     }
     _getSign(){
        const key =WaybillDetail.md5Key+""
        // console.log(key)
        const {phone} =this 
        const consumerFrom=WaybillDetail.consumerFrom+""
        const {timeStamp}=this
        // console.log(timeStamp)
     //    https://blog.csdn.net/wail521/article/details/96430873       
        //  const sign  = this._parmaSort({ key ,phone,consumerFrom ,timeStamp  }) 
        const sign  = phone +consumerFrom+timeStamp+key
        // console.log("sign",sign)


         const result = crypto.createHash('md5').update(sign).digest("hex")    

        //  console.log("result",result)
         return result 
     }
 
     _getData(){
       
         const params=this._getParams()
         const sign=this._getSign()
         const {phone} = this
         const consumerFrom=WaybillDetail.consumerFrom
         const {timeStamp,waybillNumber}=this   
 
         let formData = new FormData();
         formData.append("params",params);
         formData.append("sign",sign);
         formData.append("phone",phone);
         formData.append("consumerFrom",consumerFrom);
         formData.append("timeStamp",timeStamp);
         formData.append("waybillNumber",waybillNumber);
         
 
 
 
         return  formData
 
 
     }
 
       /**
      * 
      * @param {keyObj} 将对象排序 变成 字符串
      *  例如 a=1&b=2 
      */
     _parmaSort(keyObj){
         let keyArr = [];
       for (let key in keyObj) {
         keyArr.push(key);
       }
         let reaKryArr = keyArr.sort();
         let newArrStr = "";
         for (let i = 0; i < keyArr.length; i++) {
           newArrStr += reaKryArr[i] + "=" + keyObj[reaKryArr[i]] + "&";
         }
     
         let str = newArrStr.substr(0, newArrStr.length - 1);
         return str
     }
 
    
}

export function qualified(data){
    if(data == null || data ==""||data == undefined ){
        return false
    }else{
        return true
    }
}

export function validata(data,class_type){
    let qualify =false

    let set= Object.entries(data).slice(0,class_type.IsRequire)
    let ret= set.find((value,index)=>{
        return !qualified( value[1])
    })
    if( ret == undefined){  
        //符合要求   
        if( qualified(data.phone) && qualified(data.waybillNumber ) ){  
            // switch (class_type.Name) {
            //     case 'password':
            //         if((data.loginPassward+"").length<6 || (data.loginPassward+"").length>20){
            //             Toast.fail({
            //                 message:"请输入6-20位密码"
            //             })
            //             qualify=false
            //         }else{
            //             if((data.confirmPassword+"") ==(data.loginPassward+"")){
            //                 qualify=true
            //             }else{
            //                 qualify=false
            //                 Toast.fail({
            //                     message:"密码不一致"
            //                 })
            //             }
            //         }       
                    

            //      break;               
            // }     
              qualify=true   
              return qualify
            }else{             
                return qualify
            }     
    }else{
       Toast.fail(ret[0] +" is Null");
        return false   
    }   
 


    
}


export { WaybillDetail }